package vn.com.ltv.lo.adapter;

import java.util.ArrayList;

import vn.com.ltv.lo.R;
import vn.com.ltv.lo.daos.UserDAO;
import vn.com.ltv.lo.dtos.CommentDTO;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {

	int currentPosition;
	int positionD;
	CommentHolder holder = null;
	private Context context;
	private LayoutInflater inflater = null;
	private ArrayList<CommentDTO> listCommentDTO= new ArrayList<CommentDTO>();	

	View view;

	public CommentAdapter(Context context,ArrayList<CommentDTO> listCommentDTO) {		
		this.listCommentDTO = listCommentDTO;		
		this.context = context;
		inflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return listCommentDTO.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		TextView tvUsername;
		TextView tvDesc;	
		
		view = convertView;
		currentPosition = position;
		if (convertView == null) {
			view = inflater.inflate(R.layout.comment_row, null);
			holder = new CommentHolder();
			tvUsername = (TextView) view.findViewById(R.id.comment_row_tvUsername);
			tvDesc = (TextView) view.findViewById(R.id.comment_row_tvDesc);

			holder.tvUsername = tvUsername;
			holder.tvDesc = tvDesc;			

			view.setTag(holder);
		} else {
			CommentHolder holder = (CommentHolder) view.getTag();
			tvUsername = holder.tvUsername;
			tvDesc = holder.tvDesc;
		}

		// ------load data--------
		
		UserDAO userDAO = new UserDAO(view.getContext());
		tvDesc.setText(listCommentDTO.get(position).getCmtDesc());	
		String sUserId = listCommentDTO.get(position).getUsrId();
//		Log.d("sUserId->>>", sUserId);
		tvUsername.setText(userDAO.getByUserId(sUserId).getUsrNm());

		return view;
	}

	private class CommentHolder {
		TextView tvUsername;
		TextView tvDesc;		
	}

}